# Test functions for mark correlation functions
# 
# Author: Guochun
# Data:   2011-12-18
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

#test for phylogenetic mark correlation function
test.markcorr=function(){
	mydata("phylo_mark_data")
	re=markcorr(data.ppp,marks=list("mark"="sp","biva"=list("mark"="focal",from=TRUE,to="all")),
			markfun="phylo_similar",phyd=phyd,to=80,step=0.5,neach=5,scale=TRUE)
	checkTrue(is.data.frame(re))
	checkEquals(dim(re),c(80/0.5,2))
	checkTrue(!is.null(attr(re,"constant")))
}

test.markcorr_null=function(){
	n=3000
	nsp=10
	xmax=100
	ymax=100
	x=runif(n,0,xmax)
	y=runif(n,0,ymax)
	sp=sample(0:(nsp-1),n,replace=TRUE)
	tips=0:(nsp-1)
	pd=as.numeric(as.matrix(dist(1:nsp)))
	nxy=20
	nsim=100
	pvalues=rep(0,3)
	realmpd=rep(0,nxy^2)
	abadjust=1
	
	re=.C("quadratphylo",as.double(x), as.double(y), as.integer(n), as.integer(sp), 
			as.integer(tips),as.double(pd),as.integer(nxy),as.integer(nsim), 
			as.double(xmax),as.double(ymax),as.integer(nsp),
			as.double(pvalues),as.double(realmpd),as.integer(abadjust))[[13]]
	
	xi_breaks=seq(0-1e-5,xmax+1e-5,length.out=nxy+1)
	yi_breaks=seq(0-1e-5,ymax+1e-5,length.out=nxy+1)
	xi=as.numeric(cut(x,breaks=xi_breaks))
	yi=as.numeric(cut(y,breaks=yi_breaks))
	ploti=xi+(yi-1)*nxy
	com=table(ploti,sp)
	dim(pd)=c(nsp,nsp)
	rownames(pd)=colnames(pd)=0:(nsp-1)
	re2=mpd(com,pd,TRUE)
	#results showes all of them except the last can not be calculated by mpd
	#but the adjust one is different a lot
	return(re)
}